package com.ma.sort;

import com.ma.logarithm.randomArray;

import java.util.Arrays;

public class sortTest {
    /**
     * 系统排序(小到大)
     *
     * @param arr
     */
    public static void comparator(int[] arr) {
        Arrays.sort(arr);
    }

    /**
     * 比较是否完全相同
     *
     * @param arr1
     * @param arr2
     * @return
     */
    public static boolean isEqual(int[] arr1, int[] arr2) {
        if ((arr1 == arr2 && arr2 != null) || (arr1 == arr2 && arr1 != null)) {
            return false;
        }
        if (arr1 == null && arr2 == null) {
            return true;
        }
        if (arr1.length != arr2.length) {
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != arr2[i]) {
                return false;
            }
        }
        return true;
    }

    /**
     * 简单排序测试方法
     */
    public static void simpleSortTest() {
        int[] arr = randomArray.getRandomArray(100, 100);
        System.out.println("原数组:"+ Arrays.toString(arr));
        bubbleSort.bubble(arr);
        System.out.println("冒泡排序结果:" + Arrays.toString(arr));
        selectionSort.select(arr);
        System.out.println("选择排序结果:" + Arrays.toString(arr));
        insertionSort.insertion(arr);
        System.out.println("插入排序结果:" + Arrays.toString(arr));
    }

    /**
     * 利用对数器进行测试
     *
     * @param code     1:冒泡 2:选择 3:插入
     * @param testTime 测试次数
     * @param maxSize  最大长度
     * @param Maxvalue 最大数值
     */
    public static void simpleSortTestToLogarithm(int code, int testTime, int maxSize, int Maxvalue) {
        if (code > 3) {
            return;
        }
        if (code == 1) {
            System.out.println("冒泡排序:");
        }
        if (code == 2) {
            System.out.println("选择排序:");
        }
        if (code == 3) {
            System.out.println("插入排序:");
        }
        int succeed = 0;
        for (int i = 0; i < testTime; i++) {
            int[] arr1 = randomArray.getRandomArray(maxSize, Maxvalue);
            int[] arr2 = randomArray.copyArray(arr1);
            if (code == 1) {
                bubbleSort.bubble(arr1);
            }
            if (code == 2) {
                selectionSort.select(arr1);
            }
            if (code == 3) {
                insertionSort.insertion(arr1);
            }
            comparator(arr2);
            if (!isEqual(arr1, arr2)) {
                succeed++;
                System.out.println("第" + succeed + "次失败用例:");
                System.out.println(Arrays.toString(arr1));
                System.out.println(Arrays.toString(arr2));
                break;
            }
            if (succeed > 0) {
                System.out.println("共失败次数:" + succeed);
            }

        }
        if (succeed == 0) {
            System.out.println("恭喜没有失败用例!");
        }
    }
}
